package com.app.module.camera.renderer.group

import com.app.module.camera.renderer.AbstractRenderer
import com.app.module.camera.renderer.GLRenderer
import com.app.module.camera.renderer.base.OesRenderer

open class CameraRenderer : GLRenderer {

    companion object {
        // 逆时针
        val TYPE_7193 = 0
        val TYPE_1379 = 1
        val TYPE_3917 = 2
        val TYPE_9731 = 3

        // 顺时针
        val TYPE_1739 = 4
        val TYPE_3197 = 5
        val TYPE_9371 = 6
        val TYPE_7913 = 7
    }

    private var mRenderer: GLRenderer?
    private var mDefault: AbstractRenderer

    constructor(renderer: GLRenderer?): this(renderer , OesRenderer())

    constructor(renderer: GLRenderer?, default: AbstractRenderer){
        mRenderer = renderer
        mDefault = default
        setFlag(TYPE_7193)
    }

    fun getGLRenderer() : GLRenderer? = mRenderer

    fun setGLRenderer(renderer: GLRenderer){
        this.mRenderer = renderer
    }

    fun getTextureMatrix(): FloatArray = mDefault.getTextureMatrix()

    /**
     * TYPE_MOVE
     *  -1f,1f[7]      0f,1f[8]        1f,1f[9]
     *
     *
     *  -1f,0f[4]------0f,0f[5]--------1f,0f[6]
     *
     *
     *  -1f,-1f[1]     0f,-1f[2]       1f,-1f[3]
     */
    fun setFlag(flag: Int) {
        if (flag == TYPE_7193) {
            mDefault.setVertexBuffer(floatArrayOf(
                -1.0f, 1.0f,
                -1.0f, -1.0f,
                1.0f, 1.0f,
                1.0f, -1.0f))
        } else if (flag == TYPE_1379) {
            mDefault.setVertexBuffer(floatArrayOf(
                -1.0f, -1.0f,
                1.0f, -1.0f,
                -1.0f, 1.0f,
                1.0f, 1.0f))
        } else if (flag == TYPE_3917) {
            mDefault.setVertexBuffer(floatArrayOf(
                1.0f, -1.0f,
                1.0f, 1.0f,
                -1.0f, -1.0f,
                -1.0f, 1.0f))
        } else if (flag == TYPE_9731) {
            mDefault.setVertexBuffer(floatArrayOf(
                1.0f, 1.0f,
                -1.0f, 1.0f,
                1.0f, -1.0f,
                -1.0f, -1.0f))
        } else if (flag == TYPE_1739) {
            mDefault.setVertexBuffer(floatArrayOf(
                -1.0f, -1.0f,
                -1.0f, 1.0f,
                1.0f, -1.0f,
                1.0f, 1.0f))
        } else if (flag == TYPE_3197) {
            mDefault.setVertexBuffer(floatArrayOf(
                1.0f, -1.0f,
                -1.0f, -1.0f,
                1.0f, 1.0f,
                -1.0f, 1.0f))
        } else if (flag == TYPE_9371) {
            mDefault.setVertexBuffer(floatArrayOf(
                1.0f, 1.0f,
                1.0f, -1.0f,
                -1.0f, 1.0f,
                -1.0f, -1.0f))
        } else if (flag == TYPE_7913) {
            mDefault.setVertexBuffer(floatArrayOf(
                -1.0f, 1.0f,
                1.0f, 1.0f,
                -1.0f, -1.0f,
                1.0f, -1.0f))
        }
    }


    override fun create() {
        mDefault.create()
        mRenderer?.create()
    }

    override fun sizeChanged(width: Int, height: Int) {
        mDefault.sizeChanged(width, height)
        mRenderer?.sizeChanged(width, height)
    }

    override fun draw(texture: Int) {
        // 先进行Oes绘制,因为Camera获取的纹理只能使用Oes中配置好的纹理样式进行渲染
        mRenderer?.also {
            it.draw(mDefault.drawToTexture(texture))
        } ?: also {
            mDefault.draw(texture)
        }
    }

    override fun drawToTexture(texture: Int): Int {
        return mRenderer?.drawToTexture(mDefault.drawToTexture(texture)) ?: let {
            mDefault.drawToTexture(texture)
        }
    }

    override fun destroy() {
        mRenderer?.destroy()
        mDefault.destroy()
    }
}
